package com.coolgame.login.configuration.transformers;

import java.lang.reflect.Field;

import com.coolgame.login.configuration.PropertyTransformer;
import com.coolgame.login.configuration.TransformationException;


/**
 *  
 * @copyright : (C) Copyright cool game Corporation 2011-2020
 * @date:2013-6-24
 * @author:Caiyw
 * @since JDK1.6
 * @description:TODO
 */
public class EnumTransformer implements PropertyTransformer<Enum<?>> {
    /**
     * Shared instance of this transformer. It's thread-safe so no need of multiple instances
     */
    public static final EnumTransformer SHARED_INSTANCE = new EnumTransformer();

    /**
     * Trnasforms string to enum
     *
     * @param value value that will be transformed
     * @param field value will be assigned to this field
     * @return Enum object representing the value
     * @throws TransformationException if somehting went wrong
     */
    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Enum<?> transform(String value, Field field) throws TransformationException {
        Class<? extends Enum> clazz = (Class<? extends Enum>) field.getType();

        try {
            return Enum.valueOf(clazz, value);
        }
        catch (Exception e) {
            throw new TransformationException(e);
        }
    }
}
